package com.kwan.widget.float_view.floatwindow;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;

import androidx.core.view.ViewCompat;
import timber.log.Timber;

public class FloatApp extends FloatView {

	private View mView;
	private FrameLayout mContainer;

	public static FrameLayout.LayoutParams mLayoutParams;

	public FloatApp(Context context) {
		mLayoutParams = new FrameLayout.LayoutParams(
				RelativeLayout.LayoutParams.WRAP_CONTENT,
				RelativeLayout.LayoutParams.WRAP_CONTENT);

	}

	@Override
	void setSize(int width, int height) {

		mLayoutParams.width = width;
		mLayoutParams.height = height;


	}

	@Override
	void setView(View view) {
		mView = view;
	}

	@Override
	void setGravity(int gravity, int xOffset, int yOffset) {
		mLayoutParams.gravity = gravity;
		mX = xOffset;
		mY = yOffset;
	}

	@Override
	void init(Activity activity) {
		Timber.d("init " + activity);
		attach(activity);
		mView.setLayoutParams(mLayoutParams);
		mView.setX(mX);
		mView.setY(mY);
	}


	private static boolean isCurrentShow;

	@Override
	boolean isShow() {
		return isCurrentShow;
	}


	@Override
	void show() {
		isCurrentShow = true;
		mView.setVisibility(View.VISIBLE);
	}

	@Override
	void hide() {
		isCurrentShow = false;
		mView.setVisibility(View.INVISIBLE);
	}

	void updateShowState() {
		Timber.e("updateShowState: "+isCurrentShow);

		mView.setX(mX);
		mView.setY(mY);

		mView.setVisibility(isCurrentShow ? View.VISIBLE : View.INVISIBLE);
	}


	@Override
	void dismiss() {

	}


	public FloatApp attach(Activity activity) {
		attach(getActivityRoot(activity));
		return this;
	}


	public FloatApp attach(FrameLayout container) {

		if (container == null || mView == null) {
			Timber.e("attach return 1");
			mContainer = container;
			return this;
		}


		Timber.e("attach mView:" + mView);

		Timber.e("attach mView parent :" + mView.getParent());


		if (mView.getParent() == container) {
			Timber.e("attach return 2");
			return this;
		}

		if (mView.getParent() != null) {
			Timber.e("attach removeView ");
			((ViewGroup) mView.getParent()).removeView(mView);
		}

		mContainer = container;
		Timber.e("attach addView");

		container.addView(mView);


		return this;
	}


	public FloatApp detach(Activity activity) {
		detach(getActivityRoot(activity));
		return this;
	}


	public FloatApp detach(FrameLayout container) {
		if (mView != null && container != null && ViewCompat.isAttachedToWindow(mView)) {
			container.removeView(mView);
		}
		if (getContainer() == container) {
			mContainer = null;
		}
		return this;
	}


	private FrameLayout getActivityRoot(Activity activity) {
		if (activity == null) {
			return null;
		}
		try {
			return activity.getWindow().getDecorView().findViewById(android.R.id.content);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	private FrameLayout getContainer() {
		if (mContainer == null) {
			return null;
		}
		return mContainer;
	}


	public static int mX, mY;

	@Override
	public void updateXY(int x, int y) {
		mX = x;
		mY = y;
		mView.setX(mX);
		mView.setY(mY);
	}

	@Override
	void updateX(int x) {
		mX = x;
		mView.setX(mX);
	}

	@Override
	void updateY(int y) {
		mY = y;
		mView.setY(mY);
	}

	@Override
	int getX() {
		return mX;
	}

	@Override
	int getY() {
		return mY;
	}
}
